package cn.sdormitory.smartclass.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.sdormitory.common.constant.CommonConstant;
import cn.sdormitory.common.utils.DateTimeUtils;
import cn.sdormitory.smartclass.dao.ClassRepairDao;
import cn.sdormitory.smartclass.entity.ClassRepair;
import cn.sdormitory.smartclass.service.ClassRepairDeviceService;
import cn.sdormitory.smartclass.service.ClassRepairService;
import cn.sdormitory.smartclass.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2021/12/22 22:45
 * @version：V1.0
 */
@Slf4j
@Service("classRepairServiceImpl")
public class ClassRepairServiceImpl extends ServiceImpl<ClassRepairDao, ClassRepair> implements ClassRepairService {
    @Resource
    private ClassRepairDeviceService classRepairDeviceService;
    @Override
    public IPage<ClassRepair> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
        String status = (String) params.get("status");
        LambdaQueryWrapper<ClassRepair> wrapper = new LambdaQueryWrapper<>();

        if (StrUtil.isNotEmpty(beginTime)) {
            wrapper.gt(ClassRepair::getCreateTime, beginTime);
        }else{
            wrapper.gt(ClassRepair::getCreateTime, DateTimeUtils.getMonthFirstDay());
        }
        if (StrUtil.isNotEmpty(endTime)) {
            wrapper.lt(ClassRepair::getCreateTime, endTime);
        }else{
            wrapper.lt(ClassRepair::getCreateTime, DateTimeUtils.getMonthLastDay());
        }
        if (StrUtil.isNotEmpty(status)) {
            wrapper.eq(ClassRepair::getStatus, status);
        }

        wrapper.apply(params.get(CommonConstant.SQL_FILTER) != null, (String) params.get(CommonConstant.SQL_FILTER));
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public ClassRepair getSdRepairById(Long id) {
        return getById(id);
    }

    @Override
    public int create(ClassRepair repair) {
        int result = this.baseMapper.insert(repair);
        //保存报修与设备关系
        classRepairDeviceService.insertRepairAndRepairDevice(repair.getId(), repair.getDeviceIds());
        return result;
    }

    @Override
    public int delete(Long id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public int update(Long id, ClassRepair repair) {
        repair.setId(id);
        //先删除报修与设备关系
        classRepairDeviceService.delAndCreateDevice(repair.getId(), repair.getDeviceIds());
        repair.setModifyTime(new Date());
        return this.baseMapper.updateById(repair);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public int updateStatus(Long id, String status) {
        ClassRepair repair = new ClassRepair();
        repair.setId(id);
        repair.setStatus(status);
        repair.setModifyTime(new Date());
        return this.baseMapper.updateById(repair);
    }

    @Override
    public ClassRepair getRepairInfoById(Long id) {
        return this.baseMapper.getRepairInfoById(id);
    }

    @Override
    public List<ClassRepairVo> getAppClassRepair(String repairDate, Long classId) {
        return this.baseMapper.getAppClassRepair(repairDate,classId);
    }

    @Override
    public List<ClassDeviceVo> getAppClassDevice() {
        return this.baseMapper.getAppClassDevice();
    }

    @Override
    public ClassRepairStaVo getAppClassRepairCount() {
        return this.baseMapper.getAppClassRepairCount();
    }

    @Override
    public List<ClassRepairInfoVo> getAppNewClassRepairList(Long classDeptId,Long classId) {
        return this.baseMapper.getAppNewClassRepairList(classDeptId,classId);
    }

    @Override
    public List<ClassRepairInfoVo> getAppProcessList(Long classDeptId,Long classId) {
        return this.baseMapper.getAppProcessList(classDeptId,classId);
    }

    @Override
    public List<ClassRepairInfoVo> getAppHistoryRepList(Long classDeptId,Long classId) {
        return this.baseMapper.getAppHistoryRepList(classDeptId,classId);
    }

    @Override
    public List<ClassRepairInfoVo> getAppHisRepListByCId(Long classId) {
        return this.baseMapper.getAppHisRepListByCId(classId);
    }

    @Override
    public List<ClassRepairInfoVo> getAppClassRepairList(Long classId) {
        return this.baseMapper.getAppClassRepairList(classId);
    }

    @Override
    public ClassRepairCountVo getClassRepairCount(Long classDeptId) {
        return this.baseMapper.getClassRepairCount(classDeptId);
    }

    @Override
    public ClassRepairCountVo getClassRepairAllCount() {
        return this.baseMapper.getClassRepairAllCount();
    }

    /**
     *PP端查询全部的班级与保修情况
     * @return
     */
    @Override
    public List<ClassRepairInfoVo> getAllClassRepairList() {
        return this.baseMapper.getAllClassRepairList();
    }
}
